package com.inxedu.os.edu.controller.teacher;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.inxedu.os.common.ObjectUtils;
import com.inxedu.os.edu.pojo.EduTeacher;
import com.inxedu.os.edu.pojo.QueryTeacher;
import com.inxedu.os.edu.pojo.SysSubject;
import com.inxedu.os.edu.service.subject.SubjectService;
import com.inxedu.os.edu.service.teacher.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@RequestMapping("/admin")
@Controller
@Api(value = "教师接口",tags = "教师接口")
public class TeacherListController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("teacher")
    public void initBinderTeacher(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("teacher.");
    }
    @InitBinder("queryTeacher")
    public void initBinderQueryTeacher(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("queryTeacher.");
    }


    /**
     * 模糊查询教师列表
     * @param queryTeacher
     * @return
     */
    @RequestMapping(value = "/teacher/list",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "查询教师列表",notes = "查询教师列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "教师名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "IS_STAR",value = "教师头衔",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "subjectId",value = "专业ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "count",value = "数量",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "teacherId",value = "教师ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "beginCreateTime",value = "开始时间",dataType = "date",paramType = "query"),
            @ApiImplicitParam(name = "endCreateTime",value = "结束时间",dataType = "date",paramType = "query"),
    @ApiImplicitParam(name = "page",value = "分页页数",dataType = "int",paramType = "query")})
    public ModelAndView toTeacherList(HttpServletRequest request, @ModelAttribute("queryTeacher")QueryTeacher queryTeacher,@RequestParam(defaultValue = "1") int page){
        ModelAndView model=new ModelAndView();
        model.setViewName("/view/inxedu/admin/teacher/teacher_list");
        PageHelper.startPage(page,10);
        List<EduTeacher> eduTeachers = teacherService.selectTeacherList(queryTeacher);
        PageInfo<EduTeacher> info=new PageInfo<EduTeacher>(eduTeachers);
        request.setAttribute("page",info);
        model.addObject("teacherList",eduTeachers);
        return model;
    }

    /**
     * 初始化修改教师列表
     */
    @RequestMapping(value = "/teacher/toUpdate/{id}",method ={RequestMethod.GET,RequestMethod.POST} )
    @ApiOperation(value = "初始化修改教师列表",notes = "初始化修改教师列表,根据id查询教师信息")
    @ApiImplicitParam(name = "id",value = "教师id",dataType = "int",paramType = "path")
    public ModelAndView toUpdateTeacher(HttpServletRequest request,@PathVariable("id")int id){
        ModelAndView model=new ModelAndView();
        model.setViewName("/view/inxedu/admin/teacher/teacher_update");
        EduTeacher teacher = teacherService.getTeacherById(id);
        model.addObject("teacher",teacher);
        //查询所有的专业
        List<SysSubject> subjectList = subjectService.getSubjectList();
        //获得讲师对应的专业
        SysSubject subject = subjectService.getSubjectBySubjectId(teacher.getSubjectId());
        model.addObject("subject",subject);
        Gson gson=new Gson();
        model.addObject("subjectList",gson.toJson(subjectList));
        return model;
    }
    /**
     * 更新讲师
     */
    @RequestMapping(value = "/teacher/update",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "更新讲师",notes = "更新讲师")
    @ApiImplicitParams({@ApiImplicitParam(name = "ID",value = "教师ID",dataType = "int",paramType = "path"),
            @ApiImplicitParam(name = "name",value = "教师名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "EDUCATION",value = "教师资历",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "CAREER",value = "教师简介",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "IS_STAR",value = "头衔 ",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "PIC_PATH",value = "图片路径",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "STATUS",value = "状态",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "CREATE_TIME",value = "创建时间",dataType = "date",paramType = "query"),
            @ApiImplicitParam(name = "UPDATE_TIME",value = "更新时间",dataType = "date",paramType = "query"),
            @ApiImplicitParam(name = "SUBJECT_ID",value = "专业ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "SORT",value = "排序",dataType = "int",paramType = "query")})
    public ModelAndView updateTeacher(HttpServletRequest request, @ModelAttribute("teacher") EduTeacher teacher){
        ModelAndView model = new ModelAndView();
        model.setViewName("redirect:/admin/teacher/list");
        teacher.setUpdateTime(new Date());
        teacherService.updateTeacher(teacher);
        return model;
    }

    /**
     * 刪除讲师
     */
    @RequestMapping(value = "/teacher/delete/{id}",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "刪除讲师",notes = "根据id刪除讲师")
    @ApiImplicitParam(name = "id",value = "教师id",dataType = "int",paramType = "path")
    public ModelAndView deleteTeacher(HttpServletRequest request, @PathVariable("id") int id) {
        ModelAndView model = new ModelAndView();
        model.setViewName("redirect:/admin/teacher/list");
        teacherService.deleteTeacherById(id);
        return model;
    }
    /**
     * 到教师添加页面
     */
    @RequestMapping(value = "/teacher/toadd",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "到教师添加页面",notes = "跳转到教师添加页面")
    public ModelAndView toAddTeacher(HttpServletRequest request) {
        ModelAndView model = new ModelAndView();
        model.setViewName("view/inxedu/admin/teacher/add_teacher");
        Gson gson=new Gson();
        List<SysSubject> subjectList = subjectService.getSubjectList();
        model.addObject("subjectList",gson.toJson(subjectList));
        return model;
    }
    /**
     *添加教师
     */
    @RequestMapping(value = "/teacher/add",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "添加教师",notes = "添加教师")
    @ApiImplicitParams({@ApiImplicitParam(name = "ID",value = "教师ID",dataType = "int",paramType = "path"),
            @ApiImplicitParam(name = "name",value = "教师名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "EDUCATION",value = "教师资历",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "CAREER",value = "教师简介",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "IS_STAR",value = "头衔 ",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "PIC_PATH",value = "图片路径",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "STATUS",value = "状态",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "CREATE_TIME",value = "创建时间",dataType = "date",paramType = "query"),
            @ApiImplicitParam(name = "UPDATE_TIME",value = "更新时间",dataType = "date",paramType = "query"),
            @ApiImplicitParam(name = "SUBJECT_ID",value = "专业ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "SORT",value = "排序",dataType = "int",paramType = "query")})
    public String addTeacher(HttpServletRequest request,@ModelAttribute("teacher") EduTeacher teacher){
        ModelAndView model = new ModelAndView();
        if (ObjectUtils.isNotNull(teacher)) {
            //状态:0正常1删除
            teacher.setStatus((byte) 0);
            //添加时间
            teacher.setCreateTime(new Date());
            teacher.setUpdateTime(new Date());
            teacherService.addTeacher(teacher);
        }
        return "redirect:/admin/teacher/list";
    }
}
